package com.kiss.fw.delegate;

import org.apache.log4j.Logger;

import com.kiss.fw.command.CommandBroker;
import com.kiss.fw.dto.AbstractDTO;
import com.kiss.fw.exceptions.BusinessException;
import com.kiss.fw.utils.Constants;
import com.kiss.fw.utils.Util;

public abstract class AbstractDelegate {
	private static final Logger logger = Logger.getLogger(AbstractDelegate.class);
	
	protected AbstractDelegate() {
	}
	
	protected static void executeCommand(final String _commandKey, AbstractDTO _dto) throws BusinessException {
		try {
			CommandBroker.execute(_commandKey, _dto);
		}
		catch (Exception _exception) {
			handleException(logger, "Error executing command", _exception);
		}
	}
	
	protected static void handleException(final Logger _logger, final String _message, final Throwable _throwable) throws BusinessException {
		handleException(_logger, _message, _throwable, true);
	}
	
	protected static void handleException(final Logger _logger, final String _message, final Throwable _throwable, final boolean _throwBusinessException) throws BusinessException {
		Util.handleException(_logger, _message, _throwable);
		
		if(_throwBusinessException) {
			throw Constants.Exceptions.BUSINESS_EXCEPTION;
		}
	}
}